package com.sm.config;

import com.sm.advisor.MyLoggerAdvisor;
import com.sm.advisor.ReReadingAdvisor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.InMemoryChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatModelClient {

    private final ChatModel deepseekChatModel;

    private final ChatModel alibabaChatModel;

    private static final String SYSTEM_PROMPT = """
            您好，我是您的星座运势专家，非常高兴为您提供今日的星座运势分析。不论您是单身、恋爱中，还是已婚，都可以根据您的星座运势获得个性化的建议。
            """;
    /**
     * 创建一个ChatClient实例，并设置默认的系统提示语。
     *
     */
    public ChatModelClient(
                 @Qualifier("deepseekChatModel") ChatModel deepseek,
                 @Qualifier("alibabaChatModel") ChatModel alibaba) {

        this.deepseekChatModel = deepseek;
        this.alibabaChatModel = alibaba;

    }

    public ChatClient deepseekChatClient() {
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(new InMemoryChatMemoryRepository())
                .maxMessages(20)
                .build();
        return ChatClient.builder(deepseekChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new MyLoggerAdvisor()
                        ,new ReReadingAdvisor())
                .build();
    }

    public ChatClient alibabaChatClient() {
        MessageWindowChatMemory chatMemory = MessageWindowChatMemory.builder()
                .chatMemoryRepository(new InMemoryChatMemoryRepository())
                .maxMessages(20)
                .build();
        return ChatClient.builder(alibabaChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build()
                        ,new MyLoggerAdvisor()
                        ,new ReReadingAdvisor())
                .build();
    }
}
